package com.wwj.admin.controller;

import com.wwj.framework.controller.BaseController;
import com.wwj.framework.utils.ReturnData;
import com.wwj.utils.*;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by xya on 16-10-27.
 */
@Controller
@RequestMapping("/file")
public class FileController extends BaseController {


//    @Value("#{resourceProperties['file.upload.root']}")
//    private String uploadRootDir;
//    @Value("#{resourceProperties['file.upload.image']}")
//    private String uploadImageDir;
//    @Value("#{resourceProperties['file.upload.image.thumbnail']}")
//    private String uploadImageThumbnailDir;
//    @Value("#{resourceProperties['prefix.resource.image']}")
//    private String prefixImage;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public ReturnData upload(@RequestParam(value = "upload",required = false)MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        try {
//            if (null == uploadRootDir || "".equals(uploadRootDir)) {
//                uploadRootDir = request.getSession().getServletContext().getRealPath("/upload") + "/";
//            }
            Map<String, Object> map = new HashMap<String, Object>();
            //修改文件名称
            String originFileName = file.getOriginalFilename();
            String suffix = originFileName.substring(originFileName.lastIndexOf(".")).toLowerCase();
            String filename = UUID.randomUUID().toString().replace("-", "") + suffix;
            //原始文件保存
            String path = OSSClientUtil.putDatePathFile(filename, file.getInputStream());
            String fullPath = OSSClientUtil.URL_PREFIX+path;

            if(".jpg".contains(suffix)
                || ".png".contains(suffix)
                || ".jpeg".contains(suffix)
                || ".gif".contains(suffix)
                || ".bmp".contains(suffix)
                    ) {
                //缩略图保存
                ByteArrayOutputStream bo = new ByteArrayOutputStream();
                //1f表示原始尺寸，0.3表示原图的3分之1尺寸，反之2f表示放大
                Thumbnails.of(file.getInputStream()).scale(0.3f).toOutputStream(bo);
                String thumbnailsPath = OSSClientUtil.putImageDatePathThmbnailFile(filename, new ByteArrayInputStream(bo.toByteArray()));
                String fullThumbnailsPath = OSSClientUtil.URL_PREFIX + thumbnailsPath;
                map.put("file_thumbnail_url", fullThumbnailsPath);
            }else{
                map.put("file_thumbnail_url", fullPath);
            }
            map.put("file_url", fullPath);

            return new ReturnData().success(map);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return new ReturnData().failure(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ReturnData().failure(e.getMessage());
        }
    }


}
